package io.renren.modules.sys.customer.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.customer.entity.BtCustomerBaseEntity;
import io.renren.modules.sys.customer.entity.BtCustomerFirstpageEntity;
import io.renren.modules.sys.customer.service.BtCustomerBaseService;
import io.renren.modules.sys.customer.service.BtCustomerFirstpageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author goudandan
 * @email resjz1@163.com
 * @date 2018-09-17 10:19:40
 */
@RestController
@RequestMapping("sys/btcustomerfirstpage")
public class BtCustomerFirstpageController extends AbstractController {
    @Autowired
    private BtCustomerFirstpageService btCustomerFirstpageService;
    @Autowired
    private BtCustomerBaseService btCustomerBaseService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:btcustomerfirstpage:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = btCustomerFirstpageService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:btcustomerfirstpage:info")
    public R info(@PathVariable("id") Long id){
        BtCustomerFirstpageEntity btCustomerFirstpage = btCustomerFirstpageService.selectById(id);

        return R.ok().put("btCustomerFirstpage", btCustomerFirstpage);
    }
    /**
     * 信息
     */
    @RequestMapping("/info")
    @RequiresPermissions("sys:btcustomerfirstpage:info")
    public R info(){
        Long uid = getUserId();
        if(uid.equals(0)){
            return R.error("非法用户！！");
        }
        BtCustomerBaseEntity btCustomerBase = btCustomerBaseService.SelectOneByUid(uid);
        if(btCustomerBase==null){
            return R.error("没有该店铺！");
        }
        BtCustomerFirstpageEntity btCustomerFirstpage = btCustomerFirstpageService.GetOnebyCustId(btCustomerBase.getId());
        return R.ok().put("btCustomerFirstpage", btCustomerFirstpage);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:btcustomerfirstpage:save")
    public R save(@RequestBody BtCustomerFirstpageEntity btCustomerFirstpage){
        Long uid = getUserId();
        if(uid.equals(0)){
            return R.error("非法用户！！");
        }
        BtCustomerBaseEntity btCustomerBase = btCustomerBaseService.SelectOneByUid(uid);
        if(btCustomerBase==null){
            return R.error("没有该店铺！");
        }
        String ec="";
        try {
            ec = URLDecoder.decode(btCustomerFirstpage.getYhGongao(),"utf-8") ;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        btCustomerFirstpage.setYhGongao(ec);
        btCustomerFirstpage.setCustId(btCustomerBase.getId());
        btCustomerFirstpageService.insert(btCustomerFirstpage);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:btcustomerfirstpage:update")
    public R update(@RequestBody BtCustomerFirstpageEntity btCustomerFirstpage){
        ValidatorUtils.validateEntity(btCustomerFirstpage);
        Long uid = getUserId();
        if(uid.equals(0)){
            return R.error("非法用户！！");
        }
        BtCustomerBaseEntity btCustomerBase = btCustomerBaseService.SelectOneByUid(uid);
        if(btCustomerBase==null){
            return R.error("没有该店铺！");
        }
        String ec="";
        try {
            ec = URLDecoder.decode(btCustomerFirstpage.getYhGongao(),"utf-8") ;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        btCustomerFirstpage.setYhGongao(ec);
        btCustomerFirstpage.setCustId(btCustomerBase.getId());
        btCustomerFirstpageService.updateAllColumnById(btCustomerFirstpage);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:btcustomerfirstpage:delete")
    public R delete(@RequestBody Long[] ids){
        btCustomerFirstpageService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
